package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.ICustomerKindService;
import com.younion.vo.CustomerKindVO;

/**
 * @author wells
*/
public class CustomerKindAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(CustomerKindAction.class);

	private ICustomerKindService customerKindService = null;

	private CustomerKindVO customerKindVO = new CustomerKindVO();

	private List<CustomerKindVO> customerKindList;// 结果集列表
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listCustomerKind")
	public String listCustomerKind() {
		try {
			Map paramMap = null;
			if (customerKindVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(customerKindVO));
			}
			// 获取总记录数
			int total = customerKindService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			customerKindList = customerKindService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
			this.addLog(2, "查询商家分类列表", "查询商家分类列表", this.getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addCustomerKind")
	public String addCustomerKind() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editCustomerKind")
	public String editCustomerKind() {
		try {
			customerKindVO = customerKindService.getById(customerKindVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveCustomerKind")
	public String saveCustomerKind() {
		try {
			customerKindVO = customerKindService.save(customerKindVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "customerKindList");
		String id = customerKindVO.getId() == null ? "" : customerKindVO.getId().toString();
		String log = "".equals(id) ? "添加商家分类" : "更新商家分类";
		int type = "".equals(id) ? 3:4;
		this.addLog(type, log+id, log,getSessionUser());
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteCustomerKind")
	public String deleteCustomerKind() {
		try {
			String pkId = request.getParameter("ids");
			customerKindService.remove(Arrays.asList(pkId.split(",")));
			String log = "删除商家分类";
			this.addLog(5, log+pkId, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "customerKindList");
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewCustomerKind")
	public String viewCustomerKind() {
		try {
			String pkId = request.getParameter("ids"); 
			customerKindVO = customerKindService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public CustomerKindVO getCustomerKindVO() {
		return customerKindVO;
	}

	public void setCustomerKindVO(CustomerKindVO customerKindVO) {
		this.customerKindVO = customerKindVO;
	}

	public void setCustomerKindService(ICustomerKindService customerKindService) {
		this.customerKindService = customerKindService;
	}

	public List<CustomerKindVO> getCustomerKindList() {
		return customerKindList;
	}

	public void setCustomerKindList(List<CustomerKindVO> customerKindList) {
		this.customerKindList = customerKindList;
	}

}